package com.service;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.regex.Matcher;

import org.apache.commons.io.FileUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.utils.MysqlTable;
import com.utils.OrmException;
import com.utils.StrUtil;
import com.utils.VelocityInitializer;
import com.utils.VelocityUtils;

public class MainFun {

	private String domainSaveDir;

	private String eventSaveDir;

	private String daoSaveDir;

	private String mapperSaveDir;

	private String htmlSaveDir;
	
	private MysqlTable table;
	
	private String author;
	
	private List<String> imports;
	
	public MainFun(String domainDir, String daoDir, String mapperDir, int maven, 
			MysqlTable table, String author) {
		super();
		String domainPg = table.getDomainPackage();
		String eventPg = table.getDomainPackage();
		String daoPg = table.getDaoPackage();
		String mapperPg = table.getMapperPackage();
		String mavenPath = File.separator;
		String htmlDir = File.separator;
		if (maven == 1) {
			mavenPath = File.separator + "src"+File.separator+"main"+File.separator+"java" + File.separator;
			htmlDir = File.separator + "src"+File.separator+"main"+File.separator+"resources" + File.separator 
					  +"templates" + File.separator +"admin" + File.separator;
		}
		domainPg = StrUtil.isEmpty(domainPg)?"":domainPg.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
		eventPg = StrUtil.isEmpty(eventPg)?"":eventPg.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
		daoPg =    StrUtil.isEmpty(daoPg)   ?"":daoPg.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
		mapperPg = StrUtil.isEmpty(mapperPg)?"":mapperPg.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
		
		String module = File.separator;
		if(!StrUtil.isEmpty(table.getModuleName())) {
			module = table.getModuleName().replaceAll("\\.", Matcher.quoteReplacement(File.separator));
			module = File.separator + module + File.separator;
		}
		
		if (!StrUtil.isEmpty(domainDir)) {
			this.domainSaveDir = domainDir + mavenPath + domainPg + module;
			this.eventSaveDir = domainDir + mavenPath + domainPg + module +"event" + File.separator;
		}
		if (!StrUtil.isEmpty(daoDir)) {
			this.daoSaveDir =    daoDir    + mavenPath + daoPg    + module;
		}
		if (!StrUtil.isEmpty(mapperDir)) {
			this.mapperSaveDir = mapperDir + mavenPath + mapperPg + module;
		}
		if (!StrUtil.isEmpty(htmlDir)) {
			this.htmlSaveDir = mapperDir + htmlDir;
		}
		
		this.table = table;
		this.author = author;
		this.imports = table.getImports();
		
	}

	public void init() throws OrmException {
		VelocityInitializer.initVelocity();
		VelocityContext context = VelocityUtils.prepareContext(table, author, imports);

		StringWriter domainSw = new StringWriter();
		StringWriter eventSw = new StringWriter();
		StringWriter daoJavaSw = new StringWriter();
		StringWriter daoXmlSw = new StringWriter();
		StringWriter mapperJavaSw = new StringWriter();
		StringWriter mapperXmlSw = new StringWriter();
		StringWriter htmlSw = new StringWriter();
		
		String domainFileName =    domainSaveDir + table.getClassName() + ".java";
		String eventFileName =     eventSaveDir + table.getClassName() + "Event.java";
		String daoJavaFileName =   daoSaveDir +    table.getClassName() + "Dao.java";
		String daoXmlFileName =    daoSaveDir +    table.getClassName() + "Dao.xml";
		String maperJavaFileName = mapperSaveDir + table.getClassName() + "Mapper.java";
		String mapperXmlFileName = mapperSaveDir + table.getClassName() + "Mapper.xml";
		String htmlFileName = htmlSaveDir + StrUtil.toFirstLow(table.getClassName()) + ".html";
		
		if (!StrUtil.isEmpty(domainSaveDir)) {
			Velocity.getTemplate(VelocityUtils.domain, "UTF-8").merge(context, domainSw);
			create(domainFileName, "domain", domainSw.toString());
		}
		
		if (!StrUtil.isEmpty(eventSaveDir)) {
			if (!new File(eventFileName).exists()) {
				Velocity.getTemplate(VelocityUtils.domainEvent, "UTF-8").merge(context, eventSw);
				create(eventFileName, "domainEvent", eventSw.toString());
			}
		}
		
		if (!StrUtil.isEmpty(daoSaveDir)) {
			Velocity.getTemplate(VelocityUtils.daoJava, "UTF-8").merge(context, daoJavaSw);
			create(daoJavaFileName, "dao", daoJavaSw.toString());
			
			Velocity.getTemplate(VelocityUtils.daoXml, "UTF-8").merge(context, daoXmlSw);
			create(daoXmlFileName, "daoXml", daoXmlSw.toString());
		}
		
		if (!StrUtil.isEmpty(mapperSaveDir)) {
			Velocity.getTemplate(VelocityUtils.mapperJava, "UTF-8").merge(context, mapperJavaSw);
			create(maperJavaFileName, "mapper", mapperJavaSw.toString());
			
			Velocity.getTemplate(VelocityUtils.mapperXml, "UTF-8").merge(context, mapperXmlSw);
			create(mapperXmlFileName, "mapperXml", mapperXmlSw.toString());
		}
		
		if (!StrUtil.isEmpty(htmlSaveDir)) {
			if (!new File(htmlFileName).exists()) {
				Velocity.getTemplate(VelocityUtils.html, "UTF-8").merge(context, htmlSw);
				//create(htmlFileName, "html", htmlSw.toString());
			}
		}
		
	}
	
	private void create(String path, String type, String txt) throws OrmException {
		if (null != path) {
			File defFile = new File(path);
			if (defFile.exists()) {
				defFile.delete();
			}
			System.out.println(path);
			try {
				FileUtils.writeByteArrayToFile(defFile,txt.getBytes("UTF-8"),false);
			} catch (UnsupportedEncodingException e) {
				throw new OrmException(type + "文件写入编码异常：" + e.getMessage());
			} catch (IOException e) {
				throw new OrmException(type + "文件写入异常：" + e.getMessage());
			}
		}
	}
	
}
